package com.thenewboston.andy;

import java.util.concurrent.TimeUnit;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.content.Context;
import android.content.ContextWrapper;
import android.app.Activity;
import android.app.Dialog;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.thenewboston.andy.*;

public class CountdownTimer extends Activity implements OnClickListener {

	
	// CountDownTimer object used to .start() counter
	private MyCounter countDownTimer;
	private boolean timerHasStarted = false;
	Button btnStart;
	TextView text, timeElapsedView;

	private final long startTime = 300000;
	private final long interval = 1000;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.countdown);

		btnStart = (Button) findViewById(R.id.btnStart);
		text = (TextView) findViewById(R.id.timer);
		btnStart.setOnClickListener((OnClickListener) this);
		countDownTimer = new MyCounter(startTime, interval);
		// text.setText(text.getText() + String.valueOf(startTime));
		

	}


	@Override
	// on button click, if timer has not started
	// start
	public void onClick(View arg0) {
		if (!timerHasStarted) {
			countDownTimer.start();
			timerHasStarted = true;
			btnStart.setText("Counter Started");

		} else {
			countDownTimer.cancel();
			timerHasStarted = false;
			btnStart.setText("RESET");

		}

	}

	// countDownTimerClass
	public class MyCounter extends CountDownTimer {

		public MyCounter(long startTime, long interval) {
			super(startTime, interval);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onFinish() {
			text.setText("Counter has finished!");

		}

		@Override
		public void onTick(long millisUntilFinished) {
			int minutes = (int) ((millisUntilFinished / ((1000 * 60)) % 60));
			int seconds = (int) (millisUntilFinished / 1000) % 60;
			text.setText("Time remain:" + "0" + minutes + "." + seconds);

		}

	}

}

